Stored
procedures are Transact-SQL statements that perform one or more actions
and are saved in the database with a name. Stored procedures, used
widely to encapsulate the logic of your database system, can accept
parameters and return values. Stored procedures are the only database
object that can update data by executing DML statements. For example, you may write a stored procedure named AddCustomer that accepts a CustomerName, EMailAddress, and PhoneNumber
parameter. The logic within this stored procedure can check that the
potential customer’s details are valid, insert a new row into the
Customers table using parameter values supplied, and then return the
CustomerID of the newly created customer.
To create a stored procedure, use the CREATE PROCEDURE statement syntax shown in Example 3. The CREATE PROCEDURE keywords can be shortened to CREATE PROC. To change the definition or options of a stored procedure, use the ALTER PROCEDURE or ALTER PROC statement.
Example 3. CREATE PROCEDURE Statement—Syntax
CREATE PROCEDURE [schema_name].stored_procedure_name[ ; procedure_number] [@parameter1_name parameter1_data_type [=default_parameter_value] [ OUT | OUTPUT] [READONLY] [@parameter2_name parameter2_data_type...] [ WITH ENCRYPTION | RECOMPILE | EXECUTE AS] AS [BEGIN] transact_sql_statements [END]
|
Stored procedures can be grouped into logical named groups. Each procedure within a group will have a unique procedure_number, while the entire group can be referred to using the procedure_name. The entire procedure group can be dropped at once using the DROP PROCEDURE statement. To use a single procedure, you can omit the procedure_number. In this case procedure_name will always be used to refer to it.
Parameters
are named variables passed into the procedure. Parameter names always
start with an @, and a data type must be specified for each parameter.
You can also use the default_parameter_value
to assign a default value to a parameter if the procedure was called
without this parameter being supplied. The most common use of procedure
parameters is to pass values to the stored procedure, so that it can
use these values within the Transact-SQL statements that comprise it.
Sometimes you must return values to the caller of your stored
procedure. To do so, mark each parameter you wish to return to the
caller as OUTPUT or OUT (the two are equivalent). If the parameter is not to be updated within the stored procedure, you can specify it as READONLY.
Similar to defining views, specifying the WITH ENCRYPTION option encrypts the stored procedure definition. Specify the WITH RECOMPILE
option to instruct the database engine never to cache the execution
plan for this stored procedure. Instead, the optimal execution plan
will be calculated every time the procedure is called. The EXECUTE AS option allows you to run the procedure as an alternative set of user credentials, different from those of the caller.
Example 4
creates and executes a stored procedure to add a new row into the Stars
table. The procedure accepts parameters for the star name, star type,
solar mass, and description; and returns the ID of the newly created
star.
Example 4. Creating and Executing a Stored Procedure
CREATE PROC AddNewStar @ID int OUT, @StarName varchar(50), @SolarMass decimal(10,2), @StarType varchar(50), @Description ntext = 'No description provided.' AS BEGIN DECLARE @NextStarID int SET @NextStarID = (SELECT MAX(StarID) FROM Stars) SET @NextStarID = @NextStarID + 1 INSERT dbo.Stars(StarID, StarName, SolarMass, StarType, Description) VALUES(@NextStarID, @StarName, @SolarMass, @StarType, @Description) SET @ID = @NextStarID END; DECLARE @NewStarID int EXECUTE AddNewStar @NewStarID OUT, 'Sigma Octantis', 5.6, 'Giant' SELECT @NewStarID as NewStarID SELECT * FROM Stars -- Results: -- (1 row(s) affected) -- NewStarID -- ----------- -- 4 -- (1 row(s) affected) -- StarID StarName SolarMass StarType Description -- ------- -------------- ----------- -------------- ------------ -- 3 Deneb 6.00 White Supermassive Deneb is the... Giant -- 1 Pollux 1.86 Orange Giant Pollux,also... -- 4 Sigma Octantis 5.60 Giant No description... -- 2 Sun 1.00 Yellow dwarf No description... --(4 row(s) affected)
|